package com.grandbroad.eim.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.easy.west.westclient.api.base.bean.VodBean;
import com.easy.west.westclient.api.hotvod.bean.VodOrLiveBean;
import com.easy.west.westclient.api.vod.bean.ClassBean;
import com.easy.west.westclient.api.vod.bean.ClassList;
import com.flyco.tablayout.SlidingTabLayout;
import com.grandbroad.eim.R;
import com.grandbroad.eim.ui.fragment.ClassFragment;
import com.grandbroad.eim.ui.fragment.VodFragment;
import com.grandbroad.eim.utils.UiUtlis;
import com.grandbroad.eim.wedgit.MyTabLayout;
import com.grandbroad.eim.wedgit.ZoomOutPageTransformer;
import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.circlenavigator.CircleNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ClipPagerTitleView;

import org.xutils.x;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * 首页适配器
 */
public class HomePageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int BANANER_TYPE = 0;
    public static final int HOT_TYPE = 1;
    public static final int CLASS_TYPE = 2;
    private int count=0;
    private List<VodOrLiveBean> vodOrLiveBeanList = new ArrayList<>();
    public List<VodBean> bananer = new ArrayList<>();
    private Activity activity;
    private MyHotAdpter hotAdpter;
    private BannerAdapter bannerAdapter;
    private List<ClassBean> classList=new ArrayList<>();
    private boolean isLooper=false;
    private VodViewPagerAdapter vodViewPagerAdapter;
    private FragmentManager fragmentManager;
    public HomePageAdapter(Context context,FragmentManager fragmentManager, AppCompatActivity activity) {
        bannerAdapter = new BannerAdapter(context,activity);
        hotAdpter = new MyHotAdpter();
        vodViewPagerAdapter = new VodViewPagerAdapter(activity);
        this.activity = activity;
        this.fragmentManager = fragmentManager;

    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = null;
        if (getItemViewType(i) == BANANER_TYPE) {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.bananer_item, viewGroup, false);
            return new BananerViewHolder(view);
        } else if (getItemViewType(i) == HOT_TYPE) {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.hot_live_item, viewGroup, false);
            return new HotViewHoler(view);
        } else if (getItemViewType(i) == CLASS_TYPE) {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.class_recycler_item, viewGroup, false);
            return new ClassViewholder(view);
        }
        return null;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return BANANER_TYPE;
        } else if (position == 1) {
            return HOT_TYPE;
        } else if (position == 3) {
            return CLASS_TYPE;
        }
        return position;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        if (viewHolder instanceof BananerViewHolder) {
            final BananerViewHolder bananerViewHolder = (BananerViewHolder) viewHolder;
            //获取图片url集合
            //拆分标题和图片
            /*
            final List<String> imgs = new ArrayList<>();
            List<String> titles = new ArrayList<>();
            for (VodBean bean:bananer)
            {
                imgs.add(bean.getCover_img());
                titles.add(bean.getName());
            }
            */
            /*
            BannerAdapter bannerAdapter = new BannerAdapter();
            bannerAdapter.setVodBeanList(bananer);
            */
            if (bananer.size() > 0) {
                //加入
                List<ImageView> viewList = new ArrayList<>();
                for (VodBean vodBean : bananer) {
                    ImageView img = new ImageView(viewHolder.itemView.getContext());
                    x.image().bind(img, vodBean.getCover_img());
                    viewList.add(img);
                }
                if (bananer.size() > 0) {
                    bannerAdapter.setImgIds(bananer);
                    //必须小于两边间距
                    //bananerViewHolder.homeBanner.setPageMargin(10);
                    bananerViewHolder.homeBanner.setOffscreenPageLimit(bananer.size());
                    bananerViewHolder.homeBanner.setAdapter(bannerAdapter);
                    //假无限循环
                    bananerViewHolder.homeBanner.setCurrentItem((Integer.MAX_VALUE / 2) - (Integer.MAX_VALUE % bananer.size()));
                    bananerViewHolder.homeBanner.setPageTransformer(true, new ZoomOutPageTransformer());
                    //配置indicator
                    //设置总数量
                    CircleNavigator circleNavigator =new CircleNavigator(bananerViewHolder.itemView.getContext());
                    circleNavigator.setCircleCount(bananer.size());
                    circleNavigator.setCircleColor(UiUtlis.getColor(R.color.TextFont));
                    bananerViewHolder.indicator.setNavigator(circleNavigator);
                    bananerViewHolder.homeBanner.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                        @Override
                        public void onPageScrolled(int i, float v, int i1) {
                            int reall = i%bananer.size();
                            bananerViewHolder.indicator.onPageScrolled(reall,v,i1);
                        }

                        @Override
                        public void onPageSelected(int i) {
                            int reall = i%bananer.size();
                            bananerViewHolder.indicator.onPageSelected(reall);

                        }

                        @Override
                        public void onPageScrollStateChanged(int i) {
                            bananerViewHolder.indicator.onPageScrollStateChanged(i
                            );
                        }
                    });
                    //开启一个线程，用于循环
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            count=count+1;
                            isLooper = true;
                            if (count==1)
                            {
                                //更新适配器时会出现两次该线程执行
                                while (isLooper){
                                    try {
                                        Thread.sleep(5000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }

                                    activity.runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            //这里是设置当前页的下一页
                                            bananerViewHolder.homeBanner.setCurrentItem(bananerViewHolder.homeBanner.getCurrentItem() + 1);
                                        }
                                    });
                                }
                            }

                        }
                    }).start();


                }
            }
        }
        if (viewHolder instanceof HotViewHoler) {
            HotViewHoler hotViewHoler = (HotViewHoler) viewHolder;
            //拆分热门直播和点播
            if (vodOrLiveBeanList.size()>0)
            {
                hotAdpter.setVodOrLiveBeanList(vodOrLiveBeanList);
                hotViewHoler.recyclerItemHotlive.setLayoutManager(new GridLayoutManager(viewHolder.itemView.getContext(),2,GridLayoutManager.VERTICAL,false));
                hotViewHoler.recyclerItemHotlive.setAdapter(hotAdpter);

            }

        }
        if (viewHolder instanceof ClassViewholder) {
            final ClassViewholder classViewholder = (ClassViewholder) viewHolder;
            //需要配置顶部tab
            if (classList.size()>0)
            {
                //不使用fragment
                vodViewPagerAdapter.setClassBeans(classList);
                vodViewPagerAdapter.notifyDataSetChanged();
                classViewholder.viewClassPager.setAdapter(vodViewPagerAdapter);
                classViewholder.magicIndicator.setViewPager(classViewholder.viewClassPager);


                /*
                CommonNavigator navigator = new CommonNavigator(classViewholder.itemView.getContext());
                navigator.setAdapter(new CommonNavigatorAdapter() {
                    @Override
                    public int getCount() {
                        return classList.size();
                    }

                    @Override
                    public IPagerTitleView getTitleView(Context context, int i) {
                        ClipPagerTitleView pagerTitleView = new ClipPagerTitleView(context);
                        pagerTitleView.setText(classList.get(i).getName());
                        pagerTitleView.setClipColor(Color.BLACK);
                        pagerTitleView.setTextSize(50f);
                        return pagerTitleView;
                    }

                    @Override
                    public IPagerIndicator getIndicator(Context context) {
                        return null;
                    }
                });
                classViewholder.magicIndicator.setNavigator(navigator);
                classViewholder.viewClassPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int i, float v, int i1) {
                        classViewholder.magicIndicator.onPageScrolled(i,v,i1);
                    }

                    @Override
                    public void onPageSelected(int i) {
                        classViewholder.magicIndicator.onPageSelected(i);
                    }

                    @Override
                    public void onPageScrollStateChanged(int i) {
                        classViewholder.magicIndicator.onPageScrollStateChanged(i);
                    }
                });
                */

            }
        }
    }

    @Override
    public int getItemCount() {
        return 3;
    }

    /**
     * 设置bananerdata
     *
     * @param veryhot
     */
    public void setBananerData(List<VodBean> veryhot) {
        bananer = veryhot;

    }

    /**
     * 设置热门推荐
     * @param vodOrLiveBeanList
     */
    public void setHotFourData(List<VodOrLiveBean> vodOrLiveBeanList) {
        this.vodOrLiveBeanList = vodOrLiveBeanList;
    }

    /**
     * 设置分类列表
     * @param classList
     */
    public void setClassList(List<ClassBean> classList) {
        this.classList = classList;
    }

    /***
     * type:bananer
     */
    class BananerViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.home_banner)
        ViewPager homeBanner;
        @BindView(R.id.magic_indicator)
        MagicIndicator indicator;
        public BananerViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * type:hotview
     */
    class HotViewHoler extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_more_live)
        TextView tvMoreLive;
        @BindView(R.id.recycler_item_hotlive)
        RecyclerView recyclerItemHotlive;

        public HotViewHoler(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * type:class
     */
    class ClassViewholder extends RecyclerView.ViewHolder {
        @BindView(R.id.magic_indicator)
        SlidingTabLayout magicIndicator;
        @BindView(R.id.view_class_pager)
        ViewPager viewClassPager;

        public ClassViewholder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

   public interface onVideoItemClickListener
    {
        void click(VodBean bean);
    }

}
